package com.cracker.websocket.server.service;

import com.cracker.websocket.server.dto.CmdDto;
import com.cracker.websocket.server.entity.UserEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.websocket.Session;
import java.io.IOException;

/**
 * 负责消息转发
 */
@Service
public class BrokerService {
    @Autowired
    private FactoryService factoryService;
    @Autowired
    private MessageService messageService;

    public void handle(Session session, String message) {
        CmdDto cmdDto = messageService.decode(message, CmdDto.class);
        cmdDto.setUserEntity((UserEntity) session.getUserProperties().get("user"));
        MessageHandler handler = factoryService.getHandler(cmdDto.getType());
        if (null != handler) {
            try {
                handler.doCmd(session, cmdDto);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
